package com.xiaoshuidi.cloud.module.bpm.service.message;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.web.config.WebProperties;
import com.xiaoshuidi.cloud.module.bpm.convert.message.BpmMessageConvert;
import com.xiaoshuidi.cloud.module.bpm.enums.message.BpmMessageEnum;
import com.xiaoshuidi.cloud.module.bpm.service.message.dto.*;
import com.xiaoshuidi.cloud.module.system.api.sms.SmsSendApi;
import com.xiaoshuidi.cloud.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * BPM 消息 Service 实现类
 *
 * @author
 */
@Service
@Validated
@Slf4j
public class BpmMessageServiceImpl implements BpmMessageService {

    @Resource
    private SmsSendApi smsSendApi;

    @Resource
    private WebProperties webProperties;

    @Override
    public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenProcessInstanceReject(BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("reason", reqDTO.getReason());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_REJECT.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("taskName", reqDTO.getTaskName());
        templateParams.put("startUserNickname", reqDTO.getStartUserNickname());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getAssigneeUserId(),
                BpmMessageEnum.TASK_ASSIGNED.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenCreateReserveTicket(MessageWhenCreateReserveTicketReqDTO createReserveTicketReqDTO) {
        SmsSendSingleToUserReqDTO reqDto = new SmsSendSingleToUserReqDTO();
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("param", "");
        reqDto.setMobile(createReserveTicketReqDTO.getMobile());
        reqDto.setTemplateCode(BpmMessageEnum.RESERVE_TICKET_ASSIGN_REMINDERS.getSmsTemplateCode());
        reqDto.setTemplateParams(templateParams);
        smsSendApi.sendSingleSmsToAdmin(reqDto);
    }

    /**
     * 预约工单发送短信
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenReserveTicket(MessageWhenReserveTicketReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("customerName", reqDTO.getCustomerName());
        templateParams.put("customerMobile", reqDTO.getCustomerMobile());
        log.info("预约工单发送短信,请求:{}", JSONObject.toJSONString(templateParams));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getUserId(),
                BpmMessageEnum.RESERVE_TICKET_REMINDERS.getSmsTemplateCode(), templateParams));
    }

    /**
     * 服务工单发送信息
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenSwoTicket(MessageWhenSwoTicketReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("roomFullName", reqDTO.getRoomFullName());
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getUserId(),
                BpmMessageEnum.SWO_TICKET_REMINDERS.getSmsTemplateCode(), templateParams));
    }

    /**
     * 维修工单信息-受理提醒
     * @param reqDTO
     */
    @Override
    public void sendMessageWhenSwoTicketRemind(MessageWhenSwoTicketRemindReqDTO reqDTO) {
        SmsSendSingleToUserReqDTO reqDto = new SmsSendSingleToUserReqDTO();
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("roomFullName", reqDTO.getRoomFullName());
        templateParams.put("param", reqDTO.getParam());
        reqDto.setMobile(reqDTO.getMobile());
        reqDto.setUserId(reqDTO.getUserId());
        reqDto.setTemplateCode(BpmMessageEnum.SWO_TICKET_RECEPTION_REMINDER.getSmsTemplateCode());
        reqDto.setTemplateParams(templateParams);
        smsSendApi.sendSingleSmsToAdmin(reqDto);
    }

    private String getProcessInstanceDetailUrl(String taskId) {
        return webProperties.getAdminUi().getUrl() + "/bpm/process-instance/detail?id=" + taskId;
    }

}
